package com.xintonghua.bussiness.ui.fragment.cube;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.Time;
import com.xintonghua.bussiness.ui.user.DianZhangTaskManageActivity;
import com.xintonghua.bussiness.util.CalendarTools;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.TimeCallback;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//开始和结束时间
//对话框
public class DialogStartAndEndTimeActivity extends BaseActivity {

    @BindView(R.id.tv_cancle)
    TextView tvCancle;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.tv_starttime)
    TextView tvStarttime;
    @BindView(R.id.tv_endtime)
    TextView tvEndtime;

    Time time = new Time();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_start_time_and_end_time);
        ButterKnife.bind(this);
    }

    String starttime = "";
    String endtime = "";

    @OnClick({R.id.tv_cancle, R.id.tv_ok, R.id.tv_starttime, R.id.tv_endtime, R.id.fl_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl_close:
            case R.id.tv_cancle:
                finish();
                break;
            case R.id.tv_ok:
                Intent intent = new Intent();
                if (TextUtils.isEmpty(starttime)) {
                    MyUtils.mToast(this, "请选择开始时间！");
                    return;
                }
                if (TextUtils.isEmpty(endtime)) {
                    MyUtils.mToast(this, "请选择结束时间！");
                    return;
                }

                try {
                    Date date = CalendarTools.getParseTime(starttime, "yyyy-MM-dd");
                    Date date1 = CalendarTools.getParseTime(endtime, "yyyy-MM-dd");
                    long tim0 = date.getTime();
                    long tim1 = date1.getTime();
                    if (tim0 > tim1) {
                        MyUtils.mToast(DialogStartAndEndTimeActivity.this, "开始时间不能大于结束时间！");
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("aaa","选择错误");
                }


                intent.putExtra("starttime", starttime);
                intent.putExtra("endtime", endtime);
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;
            case R.id.tv_starttime:
                CalendarTools.getInstance().getOnYearMonthDayPicker(DialogStartAndEndTimeActivity.this, new TimeCallback() {
                    @Override
                    public void timeCallback(String year, String month, String day) {
                        tvStarttime.setText("开始时间: " + year + "-" + month + "-" + day);
                        starttime = year + "-" + month + "-" + day;
                    }
                });
                break;
            case R.id.tv_endtime:
                CalendarTools.getInstance().getOnYearMonthDayPicker(DialogStartAndEndTimeActivity.this, new TimeCallback() {
                    @Override
                    public void timeCallback(String year, String month, String day) {
                        tvEndtime.setText("结束时间: " + year + "-" + month + "-" + day);
                        endtime = year + "-" + month + "-" + day;
                    }
                });
                break;
        }
    }
}
